package com.cz;

import cn.hutool.core.util.IdUtil;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/*
 * @Description:
 * @Author: chenzhen
 * @Date: 2023/5/19 15:03
 */
public class SendMessage {
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("default",true,null);
        producer.setNamesrvAddr("172.16.1.210:9876"); // 设置 NameServer 地址

        try {
            producer.start();

            String topic = "czbabl_test";
            String messageBody = "Hello, RocketMQ!";

            // 创建消息对象
            Message message = new Message(topic, messageBody.getBytes());

            String mqMsgId = IdUtil.getSnowflakeNextIdStr();
            message.getProperties().put("UNIQ_KEY", mqMsgId);
            message.setKeys(mqMsgId);
            // 发送消息并获取发送结果
            SendResult sendResult = producer.send(message);

            System.out.println("Message sent successfully. Result: " + sendResult);
        }catch (Exception e){
               e.printStackTrace();
        } finally {
            producer.shutdown();
        }
    }
}
